﻿import QtQuick 2.0

import QtQuick 2.6
import QtQuick.Controls 2.1
Item{
    id:rootItem
    width: 500
    height: 400
    property Item mMenu: null

    MouseArea {
        id:maAll
        anchors.fill: parent
        onClicked: {
            console.log("=============root item clicked");
            hidemenu()
        }
    }

    ComboBox {
        id: control
        model: ["剑客的女学生活","老夏的性福生活", "刘总的二胎行动", "郑总的小日子","雨田佬的大作"]
        anchors.centerIn: parent
        width: 200
        height: 30
        editable:true
        //下拉列表的每个item的样式
        delegate: ItemDelegate {
            width: control.width
            height: 30
            Text {
                text: modelData
                anchors.fill: parent
                anchors.leftMargin: 10
                color: "#21be2b"
                font: control.font
                elide: Text.ElideRight
                verticalAlignment: Text.AlignVCenter
                style: Text.Outline
                styleColor: "#F23A3A"
                renderType: Text.QtRendering
                smooth: true
            }
            highlighted: control.highlightedIndex === index
        }
        //可编辑框的样式
        contentItem: TextInput {
            leftPadding: 10
            rightPadding:5// control.indicator.width + control.spacing
            selectByMouse:true
            text: control.displayText
            font: control.font
            color: control.pressed ? "#17a81a" : "#21be2b"
            verticalAlignment: Text.AlignVCenter
            clip: true
            horizontalAlignment:TextInput.AlignLeft
        }
        //背景样式
        background: Rectangle {
            implicitWidth: 120
            implicitHeight: 40
            border.color: control.pressed ? "#17a81a" : "#21be2b"
            border.width: control.visualFocus ? 2 : 1
            radius: 2
        }
        //下拉列表样式
        popup: Popup {
            y: control.height - 1
            width: control.width
            implicitHeight: contentItem.implicitHeight
            padding: 1
            contentItem: ListView {
                clip: true
                implicitHeight: contentHeight
                model: control.popup.visible ? control.delegateModel : null
                currentIndex: control.highlightedIndex
                ScrollIndicator.vertical: ScrollIndicator { }
            }
            background: Rectangle {
                border.color: "#21be2b"
                radius: 2
            }
        }
        MouseArea {
            id:ma
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onClicked: {
                if (mouse.button === Qt.RightButton){
                    if(control.popup.opened){
                        control.popup.close();
                    }
                    createObj(rootItem);
                }else{
                    if(mMenu){
                        hidemenu()
                    }
                    if(control.popup.opened){
                        control.popup.close();
                    }else{
                        control.popup.open();
                    }
                }
            }
        }

    }
    ListModel{
        id:copyModel
        Component.onCompleted: {
            copyModel.append({name:"重命名"})
            copyModel.append({name:"删除"})
            copyModel.append({name:"拷贝"})
            copyModel.append({name:"粘贴"})
            copyModel.append({name:"剪切"})
        }
    }
    //动态创建一个右键菜单的页面
    function createObj(container){
        if(mMenu == null){
            var component = Qt.createComponent("QMLListView.qml");
            if (component.status == Component.Ready) {
                mMenu = component.createObject(container);
                mMenu.myModel = (copyModel);
                mMenu.signalClickedItem.connect(function(idx){
                    console.log("clicked index is: ",idx);
                    hidemenu()
                    disposeMenuChoose(idx);
                })
            }else{
                console.log("error===================");
            }
        }
        mMenu.width = 140;
        mMenu.height =  copyModel.count*mMenu.itemHeight+4;
        mMenu.visible = true;
        mMenu.y = control.y+control.height+2
        mMenu.x = control.x+(control.width-140)/2;
        mMenu.z = 10;
    }
    function hidemenu(){
        if(mMenu==null)return;
        mMenu.visible  = false;
        mMenu.z = 0;
    }
    //处理右键菜单各个点击的业务
    function disposeMenuChoose(idx){
        var inputObject = control.contentItem;
        if(idx == 0){
            inputObject.selectAll();
        }else if(idx == 1){
            inputObject.remove(control.contentItem.selectionStart,
                               inputObject.selectionEnd);
        }else if(idx == 2){
            inputObject.copy();
        }else if(idx == 3){
            inputObject.paste();
//            inputObject.cursorPosition = (0);
        }else if(idx == 4){
            inputObject.cut();
        }
    }
}
